package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.ChatActivity;
import com.xgkj.diyiketang.activity.UserDetailActivity;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.ViewUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by windo on 2015/12/18.
 */
public class MingShiAdapter extends BaseAdapter {

    private Context mContext;

    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();

    public MingShiAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<EatShowUserInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mList !=null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if(convertView == null || convertView.getTag() == null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.mingshi_item,parent,false);
            childHolder.headImg = (ImageView) convertView.findViewById(R.id.head_img);
            childHolder.nickNameText = (TextView) convertView.findViewById(R.id.nickname);
//            childHolder.personSignatureText = (LinearLayout) convertView.findViewById(R.id.person_signature);
            childHolder.schoolText = (TextView) convertView.findViewById(R.id.person_school);
            childHolder.fansNumText = (TextView) convertView.findViewById(R.id.fans_num);
            convertView.setTag(childHolder);
        }else{
            childHolder = (ChildHolder) convertView.getTag();
        }

        final EatShowUserInfo  fansInfo = mList.get(position);
        if(fansInfo != null){
            String headImgUrl = fansInfo.getUserLogo();
            if(headImgUrl != null && headImgUrl.length()>0){
                ViewUtil.Size size =  ViewUtil.getHeadImageSize(mContext,50);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(headImgUrl,size));
                if (cacheBitmap != null){
                    childHolder.headImg.setImageBitmap(cacheBitmap);
                }else {
                    childHolder.headImg.setImageResource(R.drawable.default_head_image);
                    childHolder.headImg.setTag(headImgUrl);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg, headImgUrl, size);
                }
//                childHolder.headImg.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        Intent intent = new Intent(mContext, UserDetailActivity.class);
//                        intent.putExtra("userId",fansInfo.getUserId());
//                        mContext.startActivity(intent);
//                    }
//                });
            }else{
                childHolder.headImg.setImageResource(R.drawable.default_head_image);
            }
            if(fansInfo.getNickName()!=null){
                try {
                    childHolder.nickNameText.setText(URLDecoder.decode(fansInfo.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if (!ViewUtil.isNull(fansInfo.getFansNum())) {
                childHolder.fansNumText.setText(fansInfo.getFansNum());
            }
            childHolder.schoolText.setText(fansInfo.getSchoolName());

            String tagString = fansInfo.getLabel();
            String[] tag = null;
            if (!ViewUtil.isNull(tagString)){
                tag = tagString.split(";");
            }

//            childHolder.personSignatureText.removeAllViews();
//
//            if (tag != null && tag.length >0){
//                for (int i=0;i<tag.length;i++){
//                    TextView textView = new TextView(mContext);
//                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,ViewUtil.dip2px(mContext,20));
//                    if (i>0){
//                        params.setMargins(ViewUtil.dip2px(mContext,5),0,0,0);
//                    }
//                    textView.setLayoutParams(params);
//                    textView.setGravity(Gravity.CENTER);
//                    textView.setTextColor(mContext.getResources().getColor(R.color.hint_color));
//                    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,10);
//                    textView.setPadding(ViewUtil.dip2px(mContext,5),0,ViewUtil.dip2px(mContext,5),0);
//                    if (Build.VERSION.SDK_INT >15){
//                        textView.setBackground(mContext.getResources().getDrawable(R.drawable.teacher_tag_bg));
//                    }else {
//                        textView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.teacher_tag_bg));
//                    }
//                    textView.setText(tag[i]);
//
//                    childHolder.personSignatureText.addView(textView);
//                }
//            }

        }

        return convertView;
    }

    class ChildHolder{
        ImageView headImg;
        TextView nickNameText;
//        LinearLayout personSignatureText;
        TextView schoolText;
        TextView fansNumText;
    }
}
